package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class listview extends AppCompatActivity {

    ListView listView;
    TextView txtResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_listview2);

        List<String> data = new ArrayList<String>();
        for (int i = 0;i < 12;i++)
            data.add("测试数据"+Integer.toString(i));
        listView = (ListView)findViewById(R.id.lvPH);
        txtResult = (TextView)findViewById(R.id.txtResult);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                txtResult.setText("第"+Long.toString(id)+"项");
            }
        });
    }
}

